package com.zhao.service.serviceImpl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.Util.IdUtils;
import com.zhao.config.AlipayConfig;
import com.zhao.pojo.Orders;
import com.zhao.pojo.Wallet;
import com.zhao.service.AlipayService;
import com.zhao.service.OrdersService;
import com.zhao.service.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.UUID;

/**
 * 业务层
 */
@Service
@Slf4j
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    OrdersService ordersService;
    @Autowired
    WalletService walletService;
    @Override
    public String aliPay(HttpServletResponse response, HttpServletRequest request, Orders orders) throws IOException {
//        response.setContentType("text/html;charset=utf-8");

//        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        aliPayRequest.setReturnUrl(AlipayConfig.return_url);
        aliPayRequest.setNotifyUrl(AlipayConfig.notify_url);


        //商户订单号，后台可以写一个工具类生成一个订单号，必填
//        String order_number = new String(UUID.randomUUID().toString().replace("-",""));
        String order_number = "YXYY"+ IdUtils.getPrimaryKey();
        //付款金额，从前台获取，必填
        String total_amount = new String(orders.getPrice()+"");
        //订单名称，必填
        String subject = new String("也许有用 金币充值"+orders.getPrice().divide(new BigDecimal(orders.getNum()))+"元*"+orders.getNum());
        //设置订单状态
        orders.setOrderStatus("待支付");
        orders.setId(order_number);//订单编号
        orders.setSubject(subject);//订单名称
        orders.setPrice(orders.getPrice());

        ordersService.saveOrUpdate(orders);
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
//        ordersService.saveOrUpdate(orders);
//        Wallet uid = walletService.getOne(new QueryWrapper<Wallet>().eq("uid", orders.getUid()));
//        uid.setGolds((Integer.parseInt(uid.getGolds())+(Integer.parseInt(total_amount)*10))+"");
//        walletService.updateById(uid);
        //输出
//        out.println(result);
        log.info("返回结果={}",result);
        return result;
    }
}
